import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Message;
import javax.jms.TextMessage;

public class MyListenerVar implements MessageListener
{
    int N = 100;
    double[] tmp = new double[N];
    int n = 0;
    double num = 0;
    double average = 0;
    double total = 0;

    public void onMessage(Message message)
    {
        try
        {
            num = Double.parseDouble(((TextMessage)message).getText());
            tmp[n%N] = num;
            ++n;
            if (n >= N)
            {
                total = 0;
                for (int i=0;i<N;++i)
                {
                    total += tmp[i];
                }
                average = total/N;
                total = 0;
                for (int i=0;i<N;++i)
                {
                    total += Math.pow(tmp[i]-average, 2);
                }
                System.out.println("Received a message: " + num + " variance: " + total / N);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
